package com.briup.smartcity_group6.web.controller;


import com.briup.smartcity_group6.bean.basic.BaseUser;
import com.briup.smartcity_group6.service.IBaseUserService;
import com.briup.smartcity_group6.utils.Result;
import com.briup.smartcity_group6.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;



@Api(tags = "用户管理模块")
@RequestMapping("/auth/user")
@RestController
public class UserController {
    @Autowired
    private IBaseUserService service;

    @ApiOperation("获取当前用户信息")
    @GetMapping("/info")
    public Result info(@RequestHeader("Authorization") String token){
        BaseUser user = service.currentUserInfo(token);
        return ResultUtil.success(user);
    }
    @ApiOperation("根据id修改用户图像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户的id",required = true),
            @ApiImplicitParam(name = "icon",value = "用户图像连接",required = true)
    })
    @PutMapping("/icon")
    public Result updateUserIcon(Integer id,String icon){
        service.updateUserIcon(id,icon);
        return ResultUtil.success("修改用户图像成功");
    }
    @ApiOperation("根据用户名和用户角色分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = false),
            @ApiImplicitParam(name = "role",value = "角色",required = false),
            @ApiImplicitParam(name = "pageNum",value = "当前页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页的大小",required = true)

    })
    @GetMapping("page")
    public Result PageFindByUnameAndRole(int pageNum, int pageSize, String username, Integer
            role){
        PageInfo<BaseUser> baseUserPageInfo = service.pageQuery(pageNum, pageSize, username, role);
        return ResultUtil.success(baseUserPageInfo);
    }
    @ApiOperation("更新用户信息")
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "id",value = "id",required = true),
    //        @ApiImplicitParam(name = "telephone",value = "telephone",required = true),
    //        @ApiImplicitParam(name = "realname",value = "realname",required = true),
    //        @ApiImplicitParam(name = "gender",value = "gender",required = true),
    //        @ApiImplicitParam(name = "dob",value = "dob",required = true),
    //        @ApiImplicitParam(name = "email",value = "email",required = true),
    //})
    @PutMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result upDate(@RequestParam(value = "id",required = true) Integer id,
                         @RequestParam(value = "telephone",required = true) String telephone,
                         @RequestParam(value = "realname",required = true)String realname,
                         @RequestParam(value = "gender",required = true)Integer gender,
                         @RequestParam(value = "dob",required = true)String dob,
                         @RequestParam(value = "email",required = true)String email){
        service.upDate(id,telephone,realname,gender,dob,email);
        return ResultUtil.success("更新成功");
        }
            @ApiOperation("根据id删除用户信息")
            @ApiImplicitParam(name = "id",value = "用户id",required = true)
            @DeleteMapping
            public Result deleteById(Integer id){
                service.deleteById(id);
                return ResultUtil.success("删除成功");
    }
}

